package webapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Reports
 */
public class Reports extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ServletConfig config;
	
	String page="ViewReports.jsp";
	
       
	

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config)

			  throws ServletException{

			 this.config=config;

			 }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("We are in Service method of Reports");
		PrintWriter out=response.getWriter(); 
		
		HttpSession session=request.getSession(false); 
		String un = (String) session.getAttribute("name");
		
        if(un!=null){
		
		
		String rm=request.getParameter("RiceMill");
		String ag=request.getParameter("Agency");
		String sp=request.getParameter("choose");
		String si=request.getParameter("StockItems");
		String sd=request.getParameter("StartDate");
		String ed=request.getParameter("EndDate");
		String msg=" ";
		System.out.println(rm);
		System.out.println(ag);
		System.out.println(sp);
		System.out.println(si);
		System.out.println(sd);
		System.out.println(ed);
		
		// Connection conn = null;
		 String url = "jdbc:mysql://localhost:3306/";
		 String dbName = "webappschema";
		 String driver = "com.mysql.jdbc.Driver";
		 String dbusername = "root";
		 String dbpassword = "webapp";
		 List dataList = new ArrayList();
		 
		 if(sp.equals("Selection") || rm.equals("Selection") || ag.equals("choose") || ed.equals("") || sd.equals(""))  
		    {
		        msg = "Please insert valid data";
		        response.setContentType("text/html");
				
				 out.println("<font size = '6' color=red>" + msg + "</font>");
		        
		    }
		 
		        else if(sp.equals("Purchase")){
		    		 try {
						 Class.forName(driver).newInstance();
						 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
						 String strQuery = ("SELECT  RiceMill.RiceMillName AS RiceMillName, Agency.AgencyName AS AgencyName, Grossweight, tareweight, Netweight, purchase.Date AS Date FROM purchase, agency, ricemill WHERE Agencyid = Agency_Agencyid AND RiceMillid = RiceMill_RiceMillid AND ricemill.RiceMillName = '" + rm + "' AND agency.AgencyName = '" + ag + "' AND purchase.Date >='" + sd + "' AND purchase.Date <= '" + ed + "'");
						 Statement st = conn.createStatement();
						 ResultSet rs = st.executeQuery(strQuery);
						 response.setContentType("text/html");
						// System.out.println(rs.next());
						 
						 while(rs.next()){
							 System.out.println(rs.getString("RiceMillName"));
							 System.out.println(rs.getString("AgencyName"));
							 System.out.println(rs.getInt("Grossweight"));
							 System.out.println(rs.getInt("tareweight"));
							 System.out.println(rs.getString("Date"));
							 System.out.println(rs.getInt("Netweight"));
							 
							 dataList.add(rs.getString("RiceMillName"));
							 dataList.add(rs.getString("AgencyName"));
							 dataList.add(rs.getInt("Grossweight"));
							 dataList.add(rs.getInt("tareweight"));
							 dataList.add(rs.getInt("Netweight"));
							 dataList.add(rs.getString("Date"));
							 
							 
							 
						 }
						 
						 rs.close();
						 st.close();
					 	 } catch (Exception e) {
					 	   e.printStackTrace();
					 	 }
			    	 request.setAttribute("data", dataList);
			    	 RequestDispatcher dispatcher = request.getRequestDispatcher("ReportPurchase.jsp");

			    	  if (dispatcher != null){

			    	  dispatcher.forward(request, response);
			    	//	msg = "Hi"
						 
						 

			    	  }
			    	  
	}

		        else {
			    	 try {
						 Class.forName(driver).newInstance();
						 Connection conn = DriverManager.getConnection(url+dbName,dbusername,dbpassword);
						 String strQuery = ("SELECT ricemill.RiceMillName AS RiceMillName, agency.AgencyName AS AgencyName, stock.StockName AS StockName, sale.Date AS Date, sale.Weight AS Weight, sale.Price AS Price FROM sale, agency, ricemill, stock WHERE Agencyid = Agency_Agencyid AND RiceMillid = RiceMill_RiceMillid AND ricemill.RiceMillName = '" + rm + "' AND agency.AgencyName = '" + ag + "' AND stock.StockName = '" + si + "' AND sale.Date >='" + sd + "' AND sale.Date <= '" + ed + "'");
						 Statement st = conn.createStatement();
						 ResultSet rs = st.executeQuery(strQuery);
						 response.setContentType("text/html");
						 
						 while(rs.next()){
							 
							 System.out.println(rs.getString("RiceMillName"));
							 System.out.println(rs.getString("AgencyName"));
							 System.out.println(rs.getString("StockName"));
							 System.out.println(rs.getString("Date"));
							 System.out.println(rs.getInt("Weight"));
							 System.out.println(rs.getInt("Price"));
							 dataList.add(rs.getString("RiceMillName"));
							 dataList.add(rs.getString("AgencyName"));
							 dataList.add(rs.getString("StockName"));
							 dataList.add(rs.getString("Date"));
							 dataList.add(rs.getInt("Weight"));
							 dataList.add(rs.getInt("Price"));
							 
						 }
						 
						 
						 rs.close();
						 st.close();
					 	 } catch (Exception e) {
					 	   e.printStackTrace();
					 	 }
			    	 request.setAttribute("data", dataList);
			    	 RequestDispatcher dispatcher = request.getRequestDispatcher("ReportSale.jsp");

			    	  if (dispatcher != null){

			    	  dispatcher.forward(request, response);
			    	//	msg = "Hi"  ;

			    	
			        
			    }
		}
        }
		 else{  
				out.print("Please login first");    
	            request.getRequestDispatcher("index.jsp").include(request, response);  
	        } 
	        out.close();
	}
}


